<template>
  <div class="sensorHome">
    <div class="title">传感器类型管理</div>
    <div class="mainTable">
      <!-- 头部模块 -->
      <div class="header">
        <div class="addSensor" @click="$router.push('/addSensorType')">添加</div>
      </div>
      <!-- 表格模块 -->
      <table cellspacing="0" cellpadding="0" class="table" v-if="this.$store.state.sensor.count">
        <thead class="head">
          <th>序号</th>
          <th>传感器名称</th>
          <th>监测类型</th>
          <th>设备描述</th>
          <th>操作</th>
        </thead>
        <tbody>
          <tr
            v-for="(item, index) in  this.$store.state.sensor.tableData"
            :key="item.deviceTypeId"
            class="tr"
          >
            <td>{{index>= 9 ? index+1 : '0'+ (index + 1)}}</td>
            <td>{{ item.deviceTypeName }}</td>
            <td>{{ item.deviceCheckType }}</td>
            <td>{{ item.deviceTypeDesc }}</td>
            <td>
              <span @click="getDeviceDetail(item)" type="primary" >查看详情</span>
              <span @click="writeDevice(item.deviceTypeId)">编辑</span>
              <span @click="deleteDevice(item)">删除</span>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 导航栏模块 -->
      <div class="block" v-if="this.$store.state.sensor.count">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="10"
          layout="total, prev, pager, next, jumper"
          :total="this.$store.state.sensor.count"
        ></el-pagination>
      </div>
      <!-- 当表头为空时 -->
      <div class="empty" v-if="!this.$store.state.sensor.count">
        <img src="../../../assets/scene/empty.png" alt class="emptyLogo" />
        <span>空空如也</span>
      </div>
      <!-- 抽屉模块 -->
      <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false" size="40%">
        <header>
          <i class="el-icon-close" @click="drawer = false"></i>
        </header>
        <div class="scroll">
          <div class="deviceInfoTitle">传感器信息</div>
          <div class="deviceInfo">
            <div class="deviceTypeName">
              <span>传感器类型名称：</span>
              <span>{{deviceDetail.deviceTypeName}}</span>
            </div>
            <div class="deviceCheckType">
              <span>监测类型：</span>
              <span>{{deviceDetail.deviceCheckType}}</span>
            </div>
            <div class="deviceTypeImg">
              <span>传感器图标：</span>
              <div class="Deviceimg">
                <img :src="deviceDetail.deviceTypeImg" alt />
              </div>
            </div>
            <div class="deviceTypeDesc">
              <span>设备描述：</span>
              <span>{{deviceDetail.deviceTypeDesc}}</span>
            </div>
          </div>
          <div class="deviceMonitor">监测数据</div>
          <footer>
            <table border="0" cellspacing="0" cellpadding="0" v-show="showTable !== 0">
              <thead>
                <th>数据名称</th>
                <th>单位</th>
                <th>键值</th>
              </thead>
              <tbody>
                <tr v-for="item in deviceDetail.deviceMonitorList" :key="item.id">
                  <td>{{item.natureValue}}</td>
                  <td>{{item.natureUnit}}</td>
                  <td>{{item.natureKey}}</td>
                </tr>
              </tbody>
            </table>
            <div class="showImg" v-show="showTable === 0">
              <img src="../../../assets/scene/empty.png" />
              <h4>空空如也</h4>
            </div>
          </footer>
        </div>
      </el-drawer>
      <delete-alert-box
      v-if="isDelete"
      :isDelete=isDelete
      :descTips=descTips
      @Confirm=Confirm
      @Cancel=Cancel
      ></delete-alert-box>
    </div>
  </div>
</template>

<script>
import { getDeviceByid, deleteDeviceByid } from '@/api/sensor'
export default {
  data () {
    return {
      index: 1,
      currentPage: +'',
      drawer: false,
      deviceDetail: [],
      showTable: '',
      ifShowDeleteBox: false,
      deleteId: '',
      descTips: [],
      isDelete: false
    }
  },
  methods: {
    async deviceTypeList () {
      await this.$store.dispatch('sensor/updateTable', this.index)
    },
    async handleCurrentChange (val) {
      await this.$store.dispatch('sensor/updateTable', val)
    },
    async getDeviceDetail (item) {
      this.drawer = true
      const { data: res } = await getDeviceByid(item.deviceTypeId)
      if (res.code === 200) {
        res.data.deviceTypeImg = await this.$_makeMediaSrc(Number(res.data.deviceTypeImg))
        this.deviceDetail = res.data
        // console.log(this.deviceDetail)
      }
      this.showTable = this.deviceDetail.deviceMonitorList.length
    },
    deleteDevice (item) {
      this.deleteId = item.deviceTypeId
      this.descTips = [{
        tips: `确定删除"${item.deviceTypeName}"场景类型？`,
        subTips: '删除之后，相关传感器信息都会删除。',
        default: ''
      }]
      this.isDelete = true
    },
    Confirm () {
      // console.log('确定')
      deleteDeviceByid(this.deleteId).then(res => {
        // console.log(res)
        if (res.data.code === 200) {
          this.$message({
            message: res.data.message,
            type: 'success',
            duration: 1000
          })
          this.deviceTypeList()
          this.isDelete = false
        } else {
          this.$message({
            message: res.data.message,
            type: 'error',
            duration: 1000
          })
        }
      })
    },
    Cancel () {
      this.isDelete = false
      // console.log('取消')
    },
    writeDevice (id) {
      sessionStorage.setItem('id', id)
      this.$router.push('/writeSensorType')
    }
  },
  created () {
    this.deviceTypeList()
  }
}
</script>

<style lang="scss" scoped>
.sensorHome {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0 30px 30px;
  overflow-y: auto;
  .title {
    position: relative;
    width: 100%;
    height: 90px;
    line-height: 90px;
    font-size: 24px;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
  }
  .mainTable {
    position: relative;
    width: 100%;
    min-height: calc(100% - 90px);
    background-color: #fff;
    .header {
      position: relative;
      height: 90px;
      border-bottom: 1px solid rgba(242, 242, 242, 1);
      .addSensor {
        cursor: pointer;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 30px;
        width: 108px;
        height: 42px;
        background: rgba(72, 104, 255, 1);
        border-radius: 21px;
        text-align: center;
        line-height: 42px;
        font-size: 16px;
        color: rgba(255, 255, 255, 1);
      }
    }
    .table {
      position: relative;
      width: 100%;
      padding: 0 30px;
      margin-top: 20px;
      .head {
        background: rgba(87, 131, 255, 0.05);
        font-size: 14px;
        font-weight: 500;
        th {
          color: rgba(51, 51, 51, 1);
          height: 60px;
          line-height: 60px;
          text-align: left;
          padding-left: 24px;
          border-bottom: solid 1px rgba(230, 230, 230, 1);
        }
      }
      tbody {
        .tr {
          line-height: 64px;
          text-align: left;
          td {
            font-size: 14px;
            font-weight: 400;
            color: rgba(102, 102, 102, 1);
            border-bottom: 1px solid #f2f2f2;
            padding-left: 24px;
            height: 64px;
            span {
              cursor: pointer;
              padding-left: 24px;
              text-decoration: underline;
              color: #4868ff;
            }
            span:first-child {
              padding-left: 0px;
            }
            span:last-child {
              color: #fb4646;
            }
          }
        }
      }
    }
    .block {
      padding: 24px 0;
      margin-right: 28px;
      overflow: hidden;
      ::v-deep .el-pagination{
        float: right;
      }
    }
    .empty {
      position: relative;
      .emptyLogo {
        position: absolute;
        top: 156px;
        left: 50%;
        transform: translateX(-50%);
        width: 240px;
        height: 240px;
      }
      span {
        font-size: 16px;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
        position: absolute;
        top: 408px;
        left: 50%;
        transform: translateX(-50%);
      }
    }
    .el-drawer {
      height: 100%;
      width: 640px;
      position: relative;
      header {
        position: relative;
        height: 56px;
        border-bottom: 1px solid #f2f2f2;
        .el-icon-close {
          cursor: pointer;
          position: absolute;
          right: 20px;
          line-height: 56px;
          font-size: 16px;
        }
      }
      .scroll {
        position: relative;
        overflow-y: auto;
        height: calc(100% - 48px);
        &::-webkit-scrollbar {
          width: 0.12rem;
          height: 0.03rem;
        }
        &::-webkit-scrollbar-thumb {
          border-radius: 0.05rem;
          background: rgba(25, 79, 216, 0.05);
        }
        &::-webkit-scrollbar-track {
          border-radius: 0;
          background: rgba(87, 131, 255, 0.05);
        }
        .deviceInfoTitle {
          width: 100%;
          height: 48px;
          background: rgba(87, 131, 255, 0.05);
          line-height: 48px;
          padding-left: 30px;
          font-size: 16px;
          color: rgba(51, 51, 51, 1);
        }
        .deviceInfo {
          height: 350px;
          width: 100%;
          .deviceTypeName,
          .deviceCheckType,
          .deviceTypeImg,
          .deviceTypeDesc {
            overflow: hidden;
            margin-top: 30px;
            span:first-child {
              width: 194px;
              float: left;
              text-align: right;
              font-size: 16px;
              font-weight: 400;
              color: rgba(102, 102, 102, 1);
            }
            span:last-child {
              width: calc(100% - 194px);
              float: right;
              font-size: 16px;
              font-weight: bold;
              color: rgba(51, 51, 51, 1);
            }
          }
          .deviceTypeImg {
            .Deviceimg {
              float: left;
              width: 108px;
              height: 108px;
              background-color: #f2f2f2;
              border-radius: 5px;
              img {
                width: 100%;
                height: 100%;
              }
            }
          }

        }
        .deviceMonitor {
          width: 100%;
          height: 48px;
          background: rgba(87, 131, 255, 0.05);
          line-height: 48px;
          padding-left: 30px;
          font-size: 16px;
          color: rgba(51, 51, 51, 1);
        }
        footer {
          position: relative;
          width: 100%;
          height: calc(100% - 540px);

          table {
            width: 100%;
            position: absolute;
            top: 40px;
            padding: 0 60px;
            thead {
              background: #f8f8f8;
              th {
                height: 50px;
                line-height: 50px;
                text-align: center;
                border: 1px solid #e6e6e6;
                font-size: 0.11667rem;
                font-weight: 400;
                color: rgba(51, 51, 51, 1);
              }
            }
            tbody {
              height: calc(100% - 49px);
              tr {
                height: 51px;
                line-height: 51px;
                td {
                  width: 154px;
                  text-overflow: ellipsis;
                  overflow: hidden;
                  text-align: center;
                  border: rgba(242, 242, 242, 1) 1px solid;
                  font-size: 0.11667rem;
                }
              }
            }
          }
          .showImg {
            img {
              position: absolute;
              width: 144px;
              height: 130px;
              left: 50%;
              top: 50%;
              transform: translate(-50%, -60%);
            }
            h4 {
              position: absolute;
              left: 50%;
              top: 75%;
              transform: translate(-50%);
            }
          }
        }
      }
    }
  }
  .deleteBox {
    position: fixed;
    top: 150px;
    // left: 460px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    width: 560px;
    height: 360px;
    background: rgba(255, 255, 255, 1);
    border-radius: 5px;
  }
}
</style>
